<?php

namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;

class JobsController extends \App\Http\Controllers\AdminController {
    /**
     * 主页
     * @return type
     */
    public function index(Request $request) {
        $job = \App\Models\Jobs::orderBy('ctime', 'asc');
        if ($request->has('status')) {
            $job->where('status', $request->input('status'));
        }
        $jobs = $job->paginate();
        $assign = compact('jobs');
        return view('admin.jobs.index', $assign);
    }
    /**
     * 新增任务
     * @param Request $request
     * @return type
     */
    public function add(Request $request) {
        if ($request->ajax()) {
            $res = \App\Models\Jobs::addJob($request);
            return \Illuminate\Support\Facades\Response::json($res);
        }
        $order = "";
        if ($request->has('oid')) {
            $order = \App\Models\Order::find($request->input('oid'));
        }
        $plots = \App\Models\Plot::where("status", 1)->where("area", $request->input('area', 1))->get();
        $area = \App\Models\Area::orderBy("sort", 'asc')->get();
        $assign = compact("area", 'request', 'plots', 'order');
        return view("admin.jobs.add", $assign);
    }
    
    /**
     * 装机师傅
     */
    public function plotiper(Request $request) {
        if ($request->has('plotid')) {
            $ipers = \App\Models\Iperplot::where('plotid', $request->input('plotid'))->leftJoin('dd_instperson', 'dd_iperplot.iperid', '=', 'dd_instperson.id')->select('dd_instperson.id','dd_instperson.name')->get();
            if (count($ipers)) {
                $res = ['status'=>1, 'info'=>$ipers];
            } else {
                $res = ['status'=>0, 'info'=>'所选小区暂时没有安排装维师傅'];
            }
        } else {
            $res = ['status'=>0, 'info'=>'没有选择小区'];
        }
        return \Illuminate\Support\Facades\Response::json($res);
    }
    /**
     * 转发工单
     * @param Request $request
     * @param type $id
     * @param type $plotid
     * @return type
     */
    public function changeReciver(Request $request, $id, $plotid) {
        $job = \App\Models\Jobs::where('id', $id)->first();
        if ($request->ajax()) {
            $update = \App\Models\Myjob::where('jobid', $id)->update(array('status'=>1));
            if (!$update) {
                return \Illuminate\Support\Facades\Response::json(array('status'=>0, 'info'=>'更新原接单人状态失败'));
            }
            $data['iperid'] = $request->input('iperid');
            $data['jobid']  = $id;
            $data['ctime'] = time();
            $res = \App\Models\Myjob::firstOrCreate($data);
            if ($res) {
                $job->status = 1;
                $job->save();
                return \Illuminate\Support\Facades\Response::json(array('status' => 1, 'info' => "转派任务成功"));
            } else {
                return \Illuminate\Support\Facades\Response::json(array('status' => 0, 'info' => '转派任务失败'));
            }
        }
        $ipers = \App\Models\Iperplot::where('plotid', $plotid)->leftJoin('dd_instperson', 'dd_iperplot.iperid', '=', 'dd_instperson.id')->select('dd_instperson.id','dd_instperson.name')->get();
        $assign = compact('ipers', 'job', 'request');
        return view("admin.jobs.changereciver", $assign);
    }
    
    
    
}
